import MyDialogManager from "../../lib/MyDialogManager";
import { TipManager } from "../../lib/TipManager";
import ItemBaseData from "../data/ItemBaseData";
import PlayerData from "../data/PlayerData";
import CircleCdSpScript from "../scripts/CircleCdSpScript";
import ScaleTw from "../scripts/ScaleTw";

export default class DaoJuCtr {
    private mBody:Laya.Sprite;
    public icon:Laya.Sprite;
    public bg:Laya.Sprite;
    public z1:Laya.Sprite;
    public z2:Laya.Sprite;
    public z3:Laya.Sprite;
    public cdSp:Laya.Sprite;
    public addBtn:Laya.Sprite;
    public numBtn:Laya.Button;
    public txtBg:Laya.Image;
    public gateTxt:Laya.Label;
    private mPorss:number=0;
    private mdata:ItemBaseData;
    private mType:number=0;
    private mId:number;
    private mIsUsing:boolean;
    private mIsWuJiMode:boolean;
    constructor(_body,_id:number){
        this.mBody=_body;
        this.mId=_id;
        this.setChildName();
        this.addEvent();
    }

    public show(_value:boolean){
        this.mBody.visible=_value;
    }

    public setData(_data:ItemBaseData,_type:number=0,_isWuJi:boolean=false){
        this.mdata=_data;
        this.mType=_type;
        this.mIsWuJiMode=_isWuJi;
        this.mIsUsing=false;
       this.setPross(0);
        this.fresh();
    }

    public setPross(_value:number){
        this.mPorss=_value;
        this.freshPross();
        this.z1.getComponent(ScaleTw).isRun=this.mPorss>=1;
    }

    private mClickFunc:Function;
    private mClickFuncCaller:any;
    public setClickFunc(_func:Function,_caller:any){
        this.mClickFunc=_func;
        this.mClickFuncCaller=_caller;
    }

    public fresh(){
        this.hideAll();
        let tisUnlock:boolean=this.mdata.unlockLevel<=PlayerData.getIns().currGate;
        if(this.mType==0){
            tisUnlock=true;
        }
        let ticon:any = this.mdata.icon;
        if(!tisUnlock){
            this.txtBg.visible=this.gateTxt.visible=this.bg.visible=true;
            this.gateTxt.text="第"+this.mdata.unlockLevel+"关解锁";
        }else{
            this.icon.visible=true;
            if(this.mIsWuJiMode)this.icon.texture=ticon;
            else this.icon.texture=ticon;
            if(this.mType !=0){
                let thasitem= PlayerData.getIns().getItemNum(this.mdata.id+"")>0;
                this.numBtn.visible=thasitem;
                this.addBtn.visible=!thasitem;
            }
           
            if(this.mType==0){
                this.cdSp.visible=this.z1.visible=true;
                this.cdSp.pos(49,48);
                this.setPross(0);
            }else{
                this.z2.visible=true;
            }
        }
    }

    public setUseSt(_value:boolean){
        this.mIsUsing=_value;
        this.z3.visible=_value;
    }

    private freshPross(){
        this.cdSp.getComponent(CircleCdSpScript).drawByPorss(this.mPorss);
    }

    private setChildName(){
        let tsp;
        for(let i=0;i<this.mBody.numChildren;i++){
            tsp= this.mBody.getChildAt(i);
            this[tsp.name]=tsp;
        }
    }

    private hideAll(){
        for(let i=0;i<this.mBody.numChildren;i++){
            this.mBody.getChildAt(i)["visible"]=false;
        } 
    }

    private addClick(){
        if(!PlayerData.getIns().isMaxAddItemVideoTimes())MyDialogManager.getIns().sceneOpen("ShopDialog.scene",false,this.mdata);
        else  TipManager.ins.showMsgAlert("已达最大次数");
    }

    private iconClick(){
        if(this.mIsUsing)return;
        if(this.mClickFunc)this.mClickFunc.call(this.mClickFuncCaller,this.mdata,this.mId);
    }

    private addEvent(){
        this.addBtn.on(Laya.Event.CLICK,this,this.addClick);
        this.icon.on(Laya.Event.CLICK,this,this.iconClick);
    }
    private removeEvent(){
        this.addBtn.off(Laya.Event.CLICK,this,this.addClick);
        this.icon.off(Laya.Event.CLICK,this,this.iconClick);
    }

    public dispose(){
        this.removeEvent();
    }
}